﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UserModels;
using UserModels.DTO;
using UserRepositorys.Intface;

namespace UserRepositorys.Impl
{
    public class UserRepository : Repository<UserInfoModel>, IUserRepository
    {
        private readonly IDeptRepository deptRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        public UserRepository(ISqlSugarClient db, IDeptRepository deptRepository) : base(db)
        {
            this.deptRepository = deptRepository;
        }

        /// <summary>
        /// 添加用户信息
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> AddUser(UserInfoModel userInfo)
        {
            return await base.Addasync(userInfo);
        }

        /// <summary>
        /// 查询全部用户
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<UserDTO>> GetAllUsers()
        {
            var userList = await base.GetAllasync();
            var roleList = await deptRepository.GetDeptList();
            var list = (from user in userList
                        join role in roleList
                        on user.DeptId equals role.DeptId
                        select new UserDTO
                        {
                            UserId=user.UserId,
                            UserName = user.UserName,
                            UserPhone = user.UserPhone,
                            UserEmail = user.UserEmail,
                            UserSex = user.UserSex,
                            DeptName = role.DeptName,
                        });
            //if (文本框有内容)
            //{
            //    list = list.Where(p => p.UserPhone == "" || p.UserName.Contains("") || );
            //}
            //if (查询类型==1)//手机号
            //{
            //    list = list.Where(p => p.UserPhone == "");
            //}
            //if (查询类型 == 2)//姓名
            //{
            //    list = list.Where(p => ;
            //}
            return list.Distinct().ToList();
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="UserAccount"></param>
        /// <param name="UserPass"></param>
        /// <returns></returns>
        public async Task<UserInfoModel> GetUserInfoById(string? UserAccount, string? UserPass)
        {
            var list = await base.GetAllAsync(x => x.UserAccount == UserAccount && x.UserPass == UserPass);
            if (list.Count > 0)
            {
                return list.FirstOrDefault();
            }
            return null;
        }


        /// <summary>
        /// 删除用户信息
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public async Task<int> DeleteUser(int UserId)
        {
            return await base.Deleteasync(UserId);
        }
    }
}
